/*
 * FileProcessor.cpp
 *
 *  Created on: 2011-2-23
 *      Author:Aeolus
 *  Modified by Binda,2011-2-23 v1.1
 *
 *   Copyright   : Copyright © 2011 Binda&Aeolus
                   All Rights Reserved
 */

#include "FileProcessor.h"

bool FileProcessor::open_file(const string filename,ios::openmode mode)
	{
		infile.close();
		infile.clear();
		infile.open(filename.c_str(),mode);
		if(!infile)
			return false;
		else
			return true;
	}



 template <class Type> 	bool FileProcessor::output_data(const string filename,const vector<Type>& data)
	{
		outfile.open(filename.c_str());
		for(typename vector<Type>::const_iterator iter=data.begin() ; iter != data.end(); iter++)
		{
			outfile << *iter << endl;
		}
		return true;
	}

template <class Type> 	bool FileProcessor::FileProcessor::output_data(const string filename,const Type data)
	{
		outfile.open(filename.c_str());
		outfile << data << endl;
		return true;
	}


template <class Type> void FileProcessor::getdata(const string  filename ,vector<Type>  &data,ios::openmode mode)
	{
		bool open_file_succee =  open_file(filename,mode);
		if(open_file_succee == false)
		{
			cout<<"open file "<< filename <<" fail!"<<endl;
			return  ;
		}
		Type temp;
		while(infile >> temp)
		{
			data.push_back(temp);
		}
	}
//Added  by Binda,20011-4-6 v1.2
template<class Type> void FileProcessor::analyze(const string filename, map<Type, int> &map_count, ios::openmode mode) {
	bool open_file_succee = open_file(filename,mode);
	if (open_file_succee == false) {
		cout << "open file " << filename << " fail!" << endl;
		return;
	}

	Type temp;
	while (infile >> temp) {
		pair<map<char, int>::iterator, bool> ret = map_count.insert(make_pair(
				temp, 1));
		if (!ret.second)//如果已经存在该键
			++ret.first->second;//
	}

}

   FileProcessor::~FileProcessor()
	{
		infile.close();
		outfile.close();
	}
